package com.sleepcamel.resteasyConfigurator.components.wizard.pages;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.sleepcamel.resteasyConfigurator.model.App;
import com.sleepcamel.resteasyConfigurator.model.dbwizard.FirstPageModel;
import com.sleepcamel.resteasyConfigurator.util.FileUtils;

public class FirstPageView extends WizardPage implements PropertyChangeListener {

	private FirstPageModel firstPage;
	private Text urlTxt;
	private Text userTxt;
	private Text passTxt;
	private Text driverTxt;
	private Text outputDirectoryTxt;
	private Text projectTxt;
	private Text packageTxt;

	public FirstPageView(FirstPageModel firstPage) {
		super("DB Connection Info");
		setTitle("DB Connection Info");
		setMessage("Please provide us connection settings to your database so we can scan it.");
		this.firstPage = firstPage;
		firstPage.addPropertyChangeListener(this);
	}

	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		setControl(container);
		container.setLayout(new GridLayout(3, false));
		
		Label lblNewLabel = new Label(container, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("Database URL");
		
		urlTxt = new Text(container, SWT.BORDER);
		urlTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblUsername = new Label(container, SWT.NONE);
		lblUsername.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblUsername.setText("Username");
		
		userTxt = new Text(container, SWT.BORDER);
		userTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblPassword = new Label(container, SWT.NONE);
		lblPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblPassword.setText("Password");
		
		passTxt = new Text(container, SWT.BORDER | SWT.PASSWORD);
		passTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblDriver = new Label(container, SWT.NONE);
		lblDriver.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblDriver.setText("Driver");
		
		driverTxt = new Text(container, SWT.BORDER);
		driverTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		
		Label lblOutputDirectory = new Label(container, SWT.NONE);
		lblOutputDirectory.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblOutputDirectory.setText("Output Directory");
		
		outputDirectoryTxt = new Text(container, SWT.BORDER);
		outputDirectoryTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		outputDirectoryTxt.setEditable(false);
		
		Button btnNewButton = new Button(container, SWT.NONE);
		btnNewButton.setText("Browse");
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				File directoryToSave = FileUtils.getDirectoryToSave(App.getInstance().getShell(), "Please provide directory where to save generated files");
				if ( directoryToSave != null ){
					firstPage.setOutputDirectory(directoryToSave);
				}
			}
		});
		
		Label projectNameLbl = new Label(container, SWT.NONE);
		projectNameLbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		projectNameLbl.setText("Project Name");
		
		projectTxt = new Text(container, SWT.BORDER);
		projectTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label packageNameLbl = new Label(container, SWT.NONE);
		packageNameLbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		packageNameLbl.setText("Package Name");
		
		packageTxt = new Text(container, SWT.BORDER);
		packageTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		DataBindingContext bindingContext = new DataBindingContext();
		IObservableValue urlObserveValue = BeansObservables.observeValue(firstPage.getJdbcUtils(), "url");
		IObservableValue urlTextObserveWidget = SWTObservables.observeText(urlTxt, SWT.Modify);
		bindingContext.bindValue(urlTextObserveWidget, urlObserveValue, null, null);
		
		IObservableValue userObserveValue = BeansObservables.observeValue(firstPage.getJdbcUtils(), "userName");
		IObservableValue userTextObserveWidget = SWTObservables.observeText(userTxt, SWT.Modify);
		bindingContext.bindValue(userTextObserveWidget, userObserveValue, null, null);
		
		IObservableValue passObserveValue = BeansObservables.observeValue(firstPage.getJdbcUtils(), "password");
		IObservableValue passTextObserveWidget = SWTObservables.observeText(passTxt, SWT.Modify);
		bindingContext.bindValue(passTextObserveWidget, passObserveValue, null, null);
		
		IObservableValue driverObserveValue = BeansObservables.observeValue(firstPage.getJdbcUtils(), "driver");
		IObservableValue driverTextObserveWidget = SWTObservables.observeText(driverTxt, SWT.Modify);
		bindingContext.bindValue(driverTextObserveWidget, driverObserveValue, null, null);
		
		IObservableValue outputDirectoryObserveValue = BeansObservables.observeValue(firstPage, "outputDirectory");
		IObservableValue outputDirectoryTextObserveWidget = SWTObservables.observeText(outputDirectoryTxt, SWT.Modify);
		bindingContext.bindValue(outputDirectoryTextObserveWidget, outputDirectoryObserveValue, null, null);
		
		IObservableValue projectObserveValue = BeansObservables.observeValue(firstPage, "projectName");
		IObservableValue projectTextObserveWidget = SWTObservables.observeText(projectTxt, SWT.Modify);
		bindingContext.bindValue(projectTextObserveWidget, projectObserveValue, null, null);
		
		IObservableValue packageObserveValue = BeansObservables.observeValue(firstPage, "packageName");
		IObservableValue packageTextObserveWidget = SWTObservables.observeText(packageTxt, SWT.Modify);
		bindingContext.bindValue(packageTextObserveWidget, packageObserveValue, null, null);
	}

	@Override
	public boolean isPageComplete() {
		return firstPage.isComplete();
	}

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		setPageComplete(isPageComplete());
	}
}
